package org.cqut.system.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cqut.framework.utils.DateUtils;
import org.cqut.system.course.domain.Course;
import org.cqut.system.course.mapper.CourseMapper;
import org.cqut.system.course.service.CourseService;
import org.cqut.system.user.vo.UserVo;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 *
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
        implements CourseService {
    private final CourseMapper courseMapper;

    public CourseServiceImpl(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public List<UserVo> getStudentForCourse(String courseId) {
        return courseMapper.selectStudentForCourse(courseId);
    }

    @Override
    public void courseIsEnd() {
        //查出未结束的课程
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_status", 1);
        List<Course> list = list(queryWrapper);
        for (Course course : list) {
            //判断是否到期
            if (DateUtils.isFinished(course.getCourseEndTime()) > 0) {
                UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("course_id", course.getCourseId());
                updateWrapper.set("course_status", "2");
                update(updateWrapper);
            }
        }
    }

    @Override
    public List<Course> getCourseList(String condition) {
        List<Course> list = courseMapper.queryCourseList(condition);
        list.sort(Comparator.comparingInt(Course::getCourseStatus));
        return list;
    }

    @Override
    public List<Course> queryCourseByStudentId(String userId) {
        return courseMapper.selectCourseByStudentId(userId);
    }
}




